package net.kazed.nextaction.context.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.kazed.nextaction.ApplicationResource;
import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.database.TaskStatus;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TaskListItemView extends LinearLayout {
   private Resources resources;
   protected TextView mDescription;
   protected TextView mDueDate;
   protected TextView type;
   protected TextView projectIndicator;
   private ApplicationResource applicationResource;

   public TaskListItemView(Context androidContext, ApplicationResource applicationResource) {
      super(androidContext);
      this.applicationResource = applicationResource;
      resources = androidContext.getResources();

      LayoutInflater vi = (LayoutInflater) androidContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      vi.inflate(R.layout.task_list_item_view, this, true);

      mDescription = (TextView) findViewById(R.id.description);
      mDueDate = (TextView) findViewById(R.id.due_date);
      type = (TextView) findViewById(R.id.type);
      projectIndicator = (TextView) findViewById(R.id.projectIndicator);
   }

   public void updateView(Task task) {
      CharSequence description = task.getDescription();
      if (task.getComplete()) {
         // add strike-through for completed tasks
         SpannableString desc = new SpannableString(description);
         desc.setSpan(new StrikethroughSpan(), 0, description.length(), Spanned.SPAN_PARAGRAPH);
         description = desc;
      }
      mDescription.setText(description);
      type.setText(getResources().getString(
            applicationResource.getTaskTypeAbbreviation(task.getStatus(TaskStatus.UNPROCESSED))));
      type.setBackgroundResource(applicationResource.getTaskTypeBackground(task.getStatus(TaskStatus.UNPROCESSED)));
      if (task.getNumberOfSubTasks() == null || task.getNumberOfSubTasks() == 0) {
         projectIndicator.setVisibility(View.GONE);
      } else {
         projectIndicator.setVisibility(View.VISIBLE);
         if (task.getParentTaskId() == null) {
            projectIndicator.setText(resources.getText(R.string.task_project_abbr));
         } else {
            projectIndicator.setText(resources.getText(R.string.task_sub_project_abbr));
         }
      }
      if (task.getDueDate() != null) {
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
         mDueDate.setText(dateFormat.format(task.getDueDate()));
         Date date = new Date();
         if (date.after(task.getDueDate())) {
            // task is overdue
            mDueDate.setTypeface(Typeface.DEFAULT_BOLD);
            mDueDate.setTextColor(Color.RED);
         } else {
            mDueDate.setTypeface(Typeface.DEFAULT);
            mDueDate.setTextColor(getContext().getResources().getColor(R.drawable.date_blue));
         }
         mDueDate.setVisibility(View.VISIBLE);
      } else {
         mDueDate.setVisibility(View.GONE);
      }
   }

}
